from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import time
import requests
from lxml import html

# 设置 Edge 选项
edge_options = Options()
edge_options.add_argument("--start-maximized")  # 最大化窗口
# edge_options.add_argument("--headless")  # 无头模式（不显示浏览器）

# 初始化 Edge WebDriver
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()), options=edge_options)

# 你的账号和密码
USERNAME = "13824424035"
PASSWORD = "030815abcHE"

try:
    # 访问登录页面
    login_url = "https://cn.bing.com/search?q=%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E7%AE%97%E6%B3%95&qs=n&form=QBRE&sp=-1&lq=0&pq=%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E7%AE%97%E6%B3%95&sc=12-6&sk=&cvid=26A50C5828F043C38F1F9019731956F1"
    driver.get(login_url)
    print("正在访问登录页面...")

    # 获取题目文本
    question_element = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located(
            (By.XPATH, '//*[@id="b_topw"]/li/ul/li/div/h2/a'))
    )
    question_text = question_element.text
    print("获取到的题目内容：", question_text)

except Exception as e:
    print("操作过程中出错:", e)
    # 保存截图以便调试
    driver.save_screenshot("error.png")
    print("已保存错误截图：error.png")

finally:
    # 保持浏览器打开（手动关闭）
    # driver.quit()  # 如果想自动关闭，取消注释这行
    time.sleep(3)  # 等待页面加载
    pass